<template>
  <div v-loading="loading" class="dashboard-container">
    <el-card class="box-card">
      <TreeTool
        :tree-node="company"
        :is-root="true"
        @delDepts="getDepartments"
        @addDepts="addDepts"
        @editDepts="editDepts"
      />
    </el-card>
    <el-tree :data="departs" :default-expand-all="true" :props="defaultProps">
      <template v-slot="{data}">
        <TreeTool :tree-node="data" @delDepts="getDepartments" @addDepts="addDepts" @editDepts="editDepts" />
      </template>
    </el-tree>
    <AddDept ref="addDept" :tree-node="treeNode" :show-dialog.sync="showDialog" @getDepartments="getDepartments" />
  </div>
</template>

<script>
import TreeTool from '@/views/departments/components/TreeTool'
import { getDepartments } from '@/api/departments'
import { transData } from '@/utils'
import AddDept from '@/views/departments/components/add-dept'

export default {
  components: {
    TreeTool,
    AddDept
  },
  data() {
    return {
      loading: false, // 用来控制进度弹层的显示和隐藏
      company: { name: '信阳土埂村集团', manager: '负责人' },
      departs: [{ name: '总裁办', manager: '梅世鑫', children: [{ name: '董事会', manager: '梅梅' }] },
        { name: '行政部', manager: '邓永康' },
        { name: '人事部', manager: '李松山' }],
      defaultProps: {
        label: 'name' // 表示 从这个属性显示内容
      },
      showDialog: false,
      treeNode: {}
    }
  },
  created() {
    this.getDepartments()
  },
  methods: {
    addDepts(node) {
      this.showDialog = true
      this.treeNode = node
    },
    async getDepartments() {
      this.loading = true
      const res = await getDepartments()
      console.log(res)
      this.departs = transData(res.depts, '')
      this.company = { name: res.companyName, manager: '负责人', id: '' }
      this.loading = false
    },
    editDepts(node) {
      this.showDialog = true
      this.treeNode = node
      this.$refs.addDept.getDetails(node.id)
    }
  }
}
</script>

<style>
.dashboard-container {
  margin: 50px auto;
  width: 900px;
}

.box-card {
  background-color: #9DD0CC;
}
</style>
